package com.yxhl.zoume.core.user.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.user.presenter.passenger.UpdatePassengerPresenter;
import com.yxhl.zoume.core.user.ui.fragment.FeedbackFragment;
import com.yxhl.zoume.core.user.ui.fragment.HelpCenterFragment;
import com.yxhl.zoume.core.user.ui.fragment.MoreFragment;
import com.yxhl.zoume.core.user.ui.fragment.NotificationCenterFragment;
import com.yxhl.zoume.core.user.ui.fragment.passenger.PassengerMgmtFragment;
import com.yxhl.zoume.core.user.ui.fragment.passenger.UpdatePassengerFragment;
import com.yxhl.zoume.core.user.ui.info.UserCenterPageEnum;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.di.component.base.HasComponent;
import com.yxhl.zoume.di.component.usercenter.DaggerPassengerComponent;
import com.yxhl.zoume.di.component.usercenter.PassengerComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.usercenter.PassengerModule;
import com.yxhl.zoume.utils.IDNoUtils;
import com.yxhl.zoume.utils.RegexUtils;
import com.yxhl.zoume.utils.SoftInputUtils;

import javax.inject.Inject;

import butterknife.BindView;

public class UserContainerActivity extends BaseActivityWithToolBar implements HasComponent<PassengerComponent> {

    private static final String TAG = "UserContainerActivity";
    private static final String INTENT_EXTRA_PARAM = "intent_extra_param";


    @BindView(R.id.fl_user_container)
    FrameLayout mFrameLayout;

    //arguments
    @Inject
    UpdatePassengerPresenter mUpdatePassengerPresenter;
    /**
     * 初始化显示的内容标识
     */
    private UserCenterPageEnum mPageEnum;
    /**
     * 当前Activity显示的Fragment
     */
    private BaseFragment mCurrentFragment;
    private UpdatePassengerFragment mUpdatePassengerFragment;


    public static Intent getCallingIntent(Activity activity, UserCenterPageEnum pageEnum) {
        Intent intent = new Intent(activity, UserContainerActivity.class);
        intent.putExtra(INTENT_EXTRA_PARAM, pageEnum);
        return intent;
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mPageEnum = (UserCenterPageEnum) intent.getSerializableExtra(INTENT_EXTRA_PARAM);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent();
        initFrameContainer();
    }

    /**
     * 初始化显示内容
     */
    private void initFrameContainer() {
        addFragment(R.id.fl_user_container, fragmentProvider(mPageEnum));
    }

    public void exchangeUpdatePassengerFragmentByAdd(ZMPassenger zmPassenger) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager != null) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            Fragment fragmentByTag = fragmentManager.findFragmentByTag(UpdatePassengerFragment.class.getSimpleName());
            if (fragmentByTag != null) {
                fragmentTransaction.show(fragmentByTag);
                fragmentTransaction.hide(mCurrentFragment);
                mUpdatePassengerFragment.setStatus(zmPassenger);
            } else {
                mUpdatePassengerFragment = UpdatePassengerFragment.newInstance(zmPassenger);
                fragmentTransaction.add(R.id.fl_user_container, mUpdatePassengerFragment, UpdatePassengerFragment.class.getSimpleName());
                fragmentTransaction.hide(mCurrentFragment);
            }

            fragmentTransaction.commitAllowingStateLoss();
        }
    }

    private Fragment fragmentProvider(UserCenterPageEnum pageEnum) {
        switch (pageEnum) {
            case PASSENGER_MANAGEMENT:
                mCurrentFragment = PassengerMgmtFragment.newInstance();
                break;
            case NOTIFICATION:
                mCurrentFragment = NotificationCenterFragment.newInstance();
                break;
            case HELP_CENTER:
                mCurrentFragment = HelpCenterFragment.newInstance();
                break;
            case FEEDBACK:
                mCurrentFragment = FeedbackFragment.newInstance();
                break;
            case MORE:
                mCurrentFragment = MoreFragment.newInstance();
                break;
        }
        return mCurrentFragment;
    }


    @Override
    public void onBackPressed() {
        //关闭输入法
        SoftInputUtils.closeInputKeyboard(this);

        switch (mPageEnum) {
            case UPDATE_PASSENGER:
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.hide(mUpdatePassengerFragment);
                transaction.show(mCurrentFragment);
                transaction.commitAllowingStateLoss();

                setDirectionFlag(UserCenterPageEnum.PASSENGER_MANAGEMENT);
                setRightOperation("");
                break;
            default:
                super.onBackPressed();
                overridePendingTransition(R.anim.left_enter, R.anim.right_out);
        }

    }

    public void setDirectionFlag(UserCenterPageEnum pageEnum) {
        this.mPageEnum = pageEnum;
    }

    public void setCurrentFragment(BaseFragment currentFragment) {
        this.mCurrentFragment = currentFragment;
    }

    /**
     * 数据校验
     *
     * @return
     */
    public boolean validPassengerData(ViewGroup rootLayout, TextView passengerNameTv,
                                      TextView passengerCardIDTv, TextView passengerMobileTV,
                                      RadioGroup ticketGroup, RadioButton adultRadio, RadioButton childRadio
    ) {
        String name = passengerNameTv.getText().toString().trim();
        String IDCard = passengerCardIDTv.getText().toString().trim();
        String mobileNumber = passengerMobileTV.getText().toString().trim();
        int radioButtonId = ticketGroup.getCheckedRadioButtonId();

        if (TextUtils.isEmpty(name)) {
            snackBar(rootLayout, getString(R.string.update_passenger_name_alert));
            return false;
        }

        if (TextUtils.isEmpty(IDCard)) {
            snackBar(rootLayout, getString(R.string.update_passenger_id_card_alert));
            return false;
        }

        if (TextUtils.isEmpty(mobileNumber)) {
            snackBar(rootLayout, getString(R.string.update_passenger_mobile_alert));
            return false;
        }

        /** !!!注意取反 */
        if (!RegexUtils.isValidMobile(mobileNumber)) {
            snackBar(rootLayout, getString(R.string.update_passenger_invalid_mobile_alert));
            return false;
        }

        /** !!!注意取反 */
        if (!IDNoUtils.isValidIDNo(IDCard)) {
            snackBar(rootLayout, getString(R.string.include_passenger_id_card_error_format));
            return false;
        }

        if (!(childRadio.getId() == radioButtonId || adultRadio.getId() == radioButtonId)) {
            snackBar(rootLayout, getString(R.string.update_passenger_ticket_alert));
            return false;
        }

        return true;
    }

    public void snackBar(View parent, String msg) {
        Snackbar.make(parent, msg, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCurrentFragment = null;
    }

    @Override
    public PassengerComponent getComponent() {
        return DaggerPassengerComponent.builder().appComponent(getAppComponent())
                .passengerModule(new PassengerModule())
                .activityModule(new ActivityModule(this))
                .build();
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        if (view != null) {
            onBackPressed();
        }
    }

    @Override
    protected void onHeaderRightClick(View view) {
        switch (mPageEnum) {
            case UPDATE_PASSENGER:
                if (mUpdatePassengerFragment != null) {
                    mUpdatePassengerFragment.goToConfirm();
                }
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_user_container;
    }


    @Override
    protected void initializeInjector() {
        getComponent().inject(this);
    }

}
